package com.example.accelsimulatortest;

import android.content.Context;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.EditText;

import androidx.appcompat.app.AppCompatActivity;

import static android.hardware.Sensor.TYPE_AMBIENT_TEMPERATURE;
import static android.hardware.Sensor.TYPE_LIGHT;
import static android.hardware.Sensor.TYPE_MAGNETIC_FIELD;
import static android.hardware.Sensor.TYPE_ORIENTATION;
import static android.hardware.Sensor.TYPE_PRESSURE;

public class Sensor1 extends AppCompatActivity implements SensorEventListener {
    private SensorManager mSensorManager;
    EditText etTxt1,etGyroscope,etTemerature,etLight,etPressure;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sensor);
        etTxt1=(EditText)findViewById(R.id.txt1);
        etGyroscope=(EditText)findViewById(R.id.txt2);
        etTemerature=(EditText)findViewById(R.id.txt3);
        etLight=(EditText)findViewById(R.id.txt4);
        etPressure=(EditText)findViewById(R.id.txt5);
        mSensorManager =(SensorManager) getSystemService(Context.SENSOR_SERVICE);
        // 连接传感器模拟器
    }
    protected void onResume()	{
        super.onResume();
        // 为系统的方向传感器注册监听器
        mSensorManager.registerListener(this,
                mSensorManager.getDefaultSensor(TYPE_ORIENTATION),
                SensorManager.SENSOR_DELAY_GAME);
        //磁场传感器
        mSensorManager.registerListener(this,
                mSensorManager.getDefaultSensor(TYPE_MAGNETIC_FIELD),
                SensorManager.SENSOR_DELAY_GAME);
        //温度传感器
        mSensorManager.registerListener(this,
                mSensorManager.getDefaultSensor(TYPE_AMBIENT_TEMPERATURE),
                SensorManager.SENSOR_DELAY_GAME);
        //光传感器
        mSensorManager.registerListener(this,
                mSensorManager.getDefaultSensor(TYPE_LIGHT),
                SensorManager.SENSOR_DELAY_GAME);
        //压力传感器
        mSensorManager.registerListener(this,
                mSensorManager.getDefaultSensor(TYPE_PRESSURE),
                SensorManager.SENSOR_DELAY_GAME);
    }
    protected void onStop()	{
        // 取消注册
        mSensorManager.unregisterListener(this);
        super.onStop();
    }
    public void onSensorChanged(SensorEvent event)	{
        float[] values = event.values;
        int SensorType=event.sensor.getType();
        StringBuilder sb;
        switch (SensorType)
        {
            // 方向传感器
            case TYPE_ORIENTATION:
                sb = new StringBuilder();
                sb.append("绕Z轴转过的角度：");
                sb.append(values[0]);
                sb.append("\n绕X轴转过的角度：");
                sb.append(values[1]);
                sb.append("\n绕Y轴转过的角度：");
                sb.append(values[2]);
                etTxt1.setText(sb.toString());
                break;
            // 磁场传感器
            case TYPE_MAGNETIC_FIELD:
                sb = new StringBuilder();
                sb.append("X方向上的角度：");
                sb.append(values[0]);
                sb.append("\nY方向上的角度：");
                sb.append(values[1]);
                sb.append("\nZ方向上的角度：");
                sb.append(values[2]);
                etGyroscope.setText(sb.toString());
                break;
            // 温度传感器
            case TYPE_AMBIENT_TEMPERATURE:
                sb = new StringBuilder();
                sb.append("当前温度为：");
                sb.append(values[0]);
                etTemerature.setText(sb.toString());
                break;
            // 光传感器
            case TYPE_LIGHT:
                sb = new StringBuilder();
                sb.append("当前光的强度为：");
                sb.append(values[0]);
                etLight.setText(sb.toString());
                break;
            // 压力传感器
            case TYPE_PRESSURE:
                sb = new StringBuilder();
                sb.append("当前压力为：");
                sb.append(values[0]);
                etPressure.setText(sb.toString());
                break;
        }
    }
    @Override
    public void onAccuracyChanged(android.hardware.Sensor sensor, int accuracy) {

    }
}
